//https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
package codeRandomThoughts.Test714买卖股票的最佳时机含手续费;

//动态规划二刷
public class Solution3 {
    public int maxProfit(int[] prices, int fee) {
        //dp[i][0]:第i天不持有股票时获取的最大利润
        //dp[i][1]:第i天持有股票时获取的最大利润
        int[][] dp = new int[prices.length][2];

        dp[0][0] = 0;
        dp[0][1] = -prices[0];

        for (int i = 1; i < prices.length; i++) {
            dp[i][0] = Math.max(dp[i - 1][1] + prices[i] - fee, dp[i - 1][0]);
            dp[i][1] = Math.max(dp[i - 1][0] - prices[i], dp[i - 1][1]);
        }

        return dp[prices.length - 1][0];
    }
}
